package cn.iocoder.yudao.module.squaredancing.service.advertisement;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.squaredancing.controller.admin.advertisement.vo.*;
import cn.iocoder.yudao.module.squaredancing.dal.dataobject.advertisement.AdvertisementDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.squaredancing.dal.mysql.advertisement.AdvertisementMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.squaredancing.enums.ErrorCodeConstants.*;

/**
 * 广告数据 Service 实现类
 *
 * @author 赵云安
 */
@Service
@Validated
public class AdvertisementServiceImpl implements AdvertisementService {

    @Resource
    private AdvertisementMapper advertisementMapper;

    @Override
    public Long createAdvertisement(AdvertisementSaveReqVO createReqVO) {
        // 插入
        AdvertisementDO advertisement = BeanUtils.toBean(createReqVO, AdvertisementDO.class);
        advertisementMapper.insert(advertisement);

        // 返回
        return advertisement.getId();
    }

    @Override
    public void updateAdvertisement(AdvertisementSaveReqVO updateReqVO) {
        // 校验存在
        validateAdvertisementExists(updateReqVO.getId());
        // 更新
        AdvertisementDO updateObj = BeanUtils.toBean(updateReqVO, AdvertisementDO.class);
        advertisementMapper.updateById(updateObj);
    }

    @Override
    public void deleteAdvertisement(Long id) {
        // 校验存在
        validateAdvertisementExists(id);
        // 删除
        advertisementMapper.deleteById(id);
    }

    @Override
        public void deleteAdvertisementListByIds(List<Long> ids) {
        // 删除
        advertisementMapper.deleteByIds(ids);
        }


    private void validateAdvertisementExists(Long id) {
        if (advertisementMapper.selectById(id) == null) {
            throw exception(ADVERTISEMENT_NOT_EXISTS);
        }
    }

    @Override
    public AdvertisementDO getAdvertisement(Long id) {
        return advertisementMapper.selectById(id);
    }

    @Override
    public PageResult<AdvertisementDO> getAdvertisementPage(AdvertisementPageReqVO pageReqVO) {
        return advertisementMapper.selectPage(pageReqVO);
    }

}